package cn.chenshiwu121.controller;

import cn.chenshiwu121.pojo.Ad1Node;
import cn.chenshiwu121.pojo.Content;
import cn.chenshiwu121.service.ContentService;
import cn.chenshiwu121.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
public class PageController {

    @Value("${AD1_CATEGORY_ID}")
    private Long AD1_CATEGORY_ID; // 轮播图id
    @Value("${AD1_HEIGHT}")
    private String AD1_HEIGHT; // 轮播图宽高度
    @Value("${AD1_HEIGHT_B}")
    private String AD1_HEIGHT_B; // 轮播图高度2
    @Value("${AD1_WIDTH}")
    private String AD1_WIDTH; // 轮播图宽度
    @Value("${AD1_WIDTH_B}")
    private String AD1_WIDTH_B; // 轮播图宽度2

    @Autowired
    private ContentService service;

    /**
     * {
     *  "srcB":"http://image.taotao.com/images/2015/03/03/2015030304360302109345.jpg",
     *  "height":240,
     *  "alt":"",
     *  "width":670,
     *  "src":"http://image.taotao.com/images/2015/03/03/2015030304360302109345.jpg",
     *  "widthB":550,
     *  "href":"http://sale.jd.com/act/e0FMkuDhJz35CNt.html?cpdad=1DLSUE",
     *  "heightB":240
     * }
     */
    @RequestMapping("/index")
    public String index(Model model) {
        List<Content> contents = service.getContentListByCatId(AD1_CATEGORY_ID);
        List<Ad1Node> ad1Nodes = new ArrayList<>();
        for (Content content: contents) {
            Ad1Node adNode = new Ad1Node();
            adNode.setHeight(AD1_HEIGHT);
            adNode.setHeightB(AD1_HEIGHT_B);
            adNode.setWidth(AD1_WIDTH);
            adNode.setWidthB(AD1_WIDTH_B);
            adNode.setAlt(content.getSubTitle());
            adNode.setHref(content.getUrl());
            adNode.setSrc(content.getPic());
            adNode.setSrcB(content.getPic2());
            ad1Nodes.add(adNode);
        }
        model.addAttribute("ad1", JsonUtils.objectToJson(ad1Nodes));
        return "index";
    }

}
